package com.itheima.demo;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;

public class DemoDataSource {
    @Test
    public void testDruid()throws Exception{
        //1、创建数据库连接池对象
        DruidDataSource ds=new DruidDataSource();
        //2、设置连接数据库四大基本特征
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/db1");
        ds.setUsername("root");
        ds.setPassword("root");

        Connection connection = ds.getConnection();
        System.out.println(connection);
    }
    @Test
    public void testIocDruid()throws Exception{
        //1、创建IOC容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
        //2、从IOC容器中获取数据源对象
        DataSource ds = ac.getBean("dataSource", DataSource.class);

        Connection connection = ds.getConnection();
        System.out.println(connection);

    }
    @Test
    public void testIocC3p0()throws Exception{
        //1、创建IOC容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
        //2、从IOC容器中获取数据源对象
        DataSource ds = ac.getBean("dataSource1",DataSource.class);
        Connection connection = ds.getConnection();
        System.out.println(connection);

    }
}
